package com.gnerv.pabu.basic.core.controller;

import com.gnerv.pabu.basic.core.entity.Result;
import com.gnerv.pabu.basic.core.entity.MonitorJvm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 基础平台-部署信息
 *
 * @author zhang
 * @date 2019/11/30
 **/
@Api(tags = "部署信息")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/deploy")
public class DeployController {

    @ApiOperation("部署 信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Result saveUser() {
        Properties properties = System.getProperties();
        List<Object> list = new ArrayList<Object>(72);
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            Map<String, String> map = new HashMap<String, String>(8);
            map.put("name", entry.getKey().toString());
            map.put("value", entry.getValue().toString());
            list.add(map);
        }
        return Result.response().data("properties", list);
    }

    @RequestMapping(value = "/jvm", method = RequestMethod.GET)
    public Result jvm() {
        final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        int threadCount = threadBean.getThreadCount();
        int daemonThreadCount = threadBean.getDaemonThreadCount();
        long totalStartedThreadCount = threadBean.getTotalStartedThreadCount();
        long[] deadlockedThreads = threadBean.findDeadlockedThreads();
        MonitorJvm monitorJvm = MonitorJvm.builder()
                .count(threadCount)
                .daemonCount(daemonThreadCount)
                .totalStartedCount(totalStartedThreadCount)
                .deadlockCount(deadlockedThreads).build();

        MemoryMXBean mxb = ManagementFactory.getMemoryMXBean();
        //Heap
        System.out.println("Max:" + mxb.getHeapMemoryUsage().getMax() / 1024 / 1024 + "MB");    //Max:1776MB
        System.out.println("Init:" + mxb.getHeapMemoryUsage().getInit() / 1024 / 1024 + "MB");  //Init:126MB
        System.out.println("Committed:" + mxb.getHeapMemoryUsage().getCommitted() / 1024 / 1024 + "MB");   //Committed:121MB
        System.out.println("Used:" + mxb.getHeapMemoryUsage().getUsed() / 1024 / 1024 + "MB");  //Used:7MB
        System.out.println(mxb.getHeapMemoryUsage().toString());    //init = 132120576(129024K) used = 8076528(7887K) committed = 126877696(123904K) max = 1862270976(1818624K)

        //Non heap
        System.out.println("Max:" + mxb.getNonHeapMemoryUsage().getMax() / 1024 / 1024 + "MB");    //Max:0MB
        System.out.println("Init:" + mxb.getNonHeapMemoryUsage().getInit() / 1024 / 1024 + "MB");  //Init:2MB
        System.out.println("Committed:" + mxb.getNonHeapMemoryUsage().getCommitted() / 1024 / 1024 + "MB");   //Committed:8MB
        System.out.println("Used:" + mxb.getNonHeapMemoryUsage().getUsed() / 1024 / 1024 + "MB");  //Used:7MB
        System.out.println(mxb.getNonHeapMemoryUsage().toString());    //init = 2555904(2496K) used = 7802056(7619K) committed = 9109504(8896K) max = -1(-1K)

        List<MemoryPoolMXBean> mpxb = ManagementFactory.getMemoryPoolMXBeans();
        for (MemoryPoolMXBean memoryPoolMXBean : mpxb) {
            System.out.println("Name:" + memoryPoolMXBean.getName());
            System.out.println("Usage:" + memoryPoolMXBean.getUsage());
            System.out.println("Manager:" + String.join(",", memoryPoolMXBean.getMemoryManagerNames()));
            System.out.println("Type:" + memoryPoolMXBean.getType());
            System.out.println("--------------------------");
        }

        /**
         * mxb.getManagementSpecVersion() = "1.2"
         * mxb.getSpecName() = "Java Virtual Machine Specification"
         * mxb.getSpecVendor() = "Oracle Corporation"
         * mxb.getStartTime() = 1527587462275
         * mxb.getUptime() = 95372
         * mxb.getVmVendor() = "Oracle Corporation"
         * mxb.getObjectName() = {ObjectName@1052} "java.lang:type=Runtime"
         * mxb.getName() = "2668@Sindrol-NTB"
         */

        OperatingSystemMXBean osb = ManagementFactory.getOperatingSystemMXBean();
        System.out.println(osb.getName()); //Windows 10
        System.out.println(osb.getArch()); //amd 64
        System.out.println(osb.getAvailableProcessors()); //4
        System.out.println(osb.getVersion()); //10.0
        System.out.println(osb.getSystemLoadAverage()); //-1.0

        //1获取JVM输入参数
        List<String> list=ManagementFactory.getRuntimeMXBean().getInputArguments();
        //2.获取当前JVM进程的PID
        String name = ManagementFactory.getRuntimeMXBean().getName();
        String pid = name.split("@")[0];
        //3.获取当前系统的负载
        ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage();

        return Result.response().data("monitorJvm", monitorJvm);
    }

}
